package com.ilxqx.framework.security.handler;

import com.ilxqx.framework.system.util.ResponseUtils;
import com.ilxqx.framework.util.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.web.session.SessionInformationExpiredEvent;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * session失效处理策略
 * @author venus
 */
@Component
@Slf4j
public class FrameworkExpiredSessionStrategy implements SessionInformationExpiredStrategy {

    @Override
    public void onExpiredSessionDetected(SessionInformationExpiredEvent event) throws IOException, ServletException {
        log.info("经过漫长的等待，session过期了");
        HttpServletRequest request = event.getRequest();
        HttpServletResponse response = event.getResponse();
        ResponseUtils.response(ResultUtils.failure("会话已经失效，请重新登录"), request, response);
    }

}
